package com.woniuxy.juc.collection;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/4/12:20:16
 * <P> Description:
 */
@Slf4j(topic = "c.c")
@SuppressWarnings("all")
public class WordCountTest {

    public static void main(String[] args) {
        demo(() -> new ConcurrentHashMap<String, LongAdder>(),
                (map, words) -> {
                    words.stream()
                            .forEach(word -> {
                                //TODO 如果缺少一个key 则计算生成一个value 不缺少则返回已经存在的值
                                // 然后将key value 放入map
                                // 最后返回value
                                LongAdder value =
                                        map.computeIfAbsent(word, (key -> new LongAdder()));

                                //TODO 对累加器进行++
                                value.increment();
//                                synchronized (WordCountTest.class) {
//                                    if (map.get(word) != null) {
//                                        map.put(word, map.get(word) + 1);
//                                    } else {
//                                        map.put(word, 1);
//                                    }
//                                }
                            });
                });
    }

    private static <V> void demo(Supplier<Map<String, V>> supplier,
                                 BiConsumer<Map<String, V>, List<String>> consumer) {
        Map<String, V> counterMap = supplier.get();
        List<Thread> ts = new ArrayList<>();
        for (int i = 1; i <= 26; i++) {
            int idx = i;
            Thread thread = new Thread(() -> {
                List<String> words = readFromFile(idx);
                consumer.accept(counterMap, words);
            });
            ts.add(thread);
        }

        ts.forEach(t -> t.start());
        ts.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        System.out.println(counterMap);
    }

    static final String source = "Z:/temp";

    public static List<String> readFromFile(int i) {
        ArrayList<String> words = new ArrayList<>();
        //  new FileOutputStream(source + "/" + (i + 1) + ".txt")))) {
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(new FileInputStream(source + "/"
                        + i + ".txt")))) {
            while (true) {
                String word = in.readLine();
                if (word == null) {
                    break;
                }
                words.add(word);
            }
            return words;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
